package com.bigdata.controller;

import com.bigdata.model.Result;
import com.bigdata.model.SearchModel;
import com.bigdata.model.AjaxResult;
import com.bigdata.model.AjaxResultList;
import com.bigdata.service.mysql.JkfwUserRoleService;
import com.bigdata.vo.VoJkfwUser;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bigdata.service.mysql.JkfwUserService;
import com.bigdata.mysql.entity.JkfwUser;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * JkfwUser的路由接口服务
 * mge/app 只能做为借口服务 用户要不登录 要不用token访问
 *
 * @author
 */
@RestController
@RequestMapping(value = {"/mge/adx", "/mge/app"})
public class JkfwUserController extends BaseController {
    @Autowired
    JkfwUserService jkfwUserService;
    @Autowired
    SessionManager sessionManager;

    /**
     * @return void
     * @Author lly
     * @Date 2019/7/18 0018
     * @Description 查看内存中所有的session信息
     */
    @RequestMapping("getSessions")
    @ResponseBody
    public List<String> getSessions() {
        DefaultWebSessionManager defaultWebSessionManager = (DefaultWebSessionManager) sessionManager;
        Collection<Session> sessions = defaultWebSessionManager.getSessionDAO().getActiveSessions();
        List<String> list = new ArrayList<String>();
        for (Session session : sessions) {
            list.add(session.getId().toString());
        }
        return list;
    }

    /**
     * @param request, searchModel
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 获取用户信息
     */
    @RequestMapping("/listUser")
    @ResponseBody
    public AjaxResultList listUser(HttpServletRequest request, SearchModel searchModel) {
        AjaxResultList res = AjaxResultList.ok();
        Result r = jkfwUserService.queryPage(searchModel);
        res.setRows(r.getList());
        res.setTotal(r.getTotal());
        res.setPages(r.getPages());
        res.setPage((long) searchModel.getPageNumber());
        return res;
    }


    @RequestMapping("getUserid")
    @ResponseBody
    public AjaxResult getUser(String userid) {
        Result user = jkfwUserService.selectByUserid(userid);
        if (user != null) {
            return AjaxResult.ok(user.getData());
        } else {
            return AjaxResult.fail("用户不存在！");
        }
    }

    /**
     * 添加用户
     *
     * @param *    @param request
     * @param user
     * @return com.bigdata.model.AjaxResult
     * @author lly
     * @date 2020/3/22 18:31
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public AjaxResult addUser(HttpServletRequest request, VoJkfwUser user) {
        Result res = jkfwUserService.addUserRole(user);
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.fail(res.getMessage());
        }
        return AjaxResult.ok();
    }

    /**
     * @param request, user
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 添加用户和权限
     */
    @RequestMapping("/addUserRole")
    @ResponseBody
    public AjaxResult addUserRole(HttpServletRequest request, VoJkfwUser user) {
        Result res = jkfwUserService.addUserRole(user);
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.fail(res.getMessage());
        }
        return AjaxResult.ok();
    }

    @RequestMapping("ckUsername")
    @ResponseBody
    public AjaxResult checkUserName(String username) {
        AjaxResult result = AjaxResult.ok();
        result.setData(jkfwUserService.ckUserName(username));
        return result;
    }

    /**
     * @param request, user
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 修改用户
     */
    @PostMapping("/updateUser")
    @ResponseBody
    public AjaxResult updateUser(HttpServletRequest request, JkfwUser user) {
        Result res = jkfwUserService.updateUser(user);
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.fail(res.getMessage());
        }
        return AjaxResult.ok();
    }

    @PostMapping("/updateUserRole")
    @ResponseBody
    public AjaxResult updateUserRole(HttpServletRequest request, VoJkfwUser user) {
        Result res = jkfwUserService.updateUserRole(user);
        if (res.getCode() != Result.Code_OK) {
            return AjaxResult.fail(res.getMessage());
        }
        return AjaxResult.ok();
    }

    /**
     * @param request, userid
     * @return com.bigdata.vo.AjaxResult
     * @Author lly
     * @Date 2019/7/15 0015
     * @Description 删除用户
     */
    @PostMapping("delUser")
    @ResponseBody
    public AjaxResult delUser(HttpServletRequest request, String userid) {
        jkfwUserService.delUser(userid);
        return AjaxResult.ok();
    }
//    /**
//     * 通过id删除JkfwUser数据方法
//     *
//     * @param id
//     * @return
//     */
//    @DeleteMapping(value = "/JkfwUser/{id}", produces = {"application/json;charset=UTF-8"})
//    public String delete(@PathVariable(name = "id") Integer id) {
//        return jkfwUserService.deleteById(id);
//    }
}
